/**
 * @author tom
 */
var EXPORTED_SYMBOLS = ['g_SyncAPI'];

Components.utils.import("resource://gsync/utils.js");
Components.utils.import("resource://gsync/gsyncplaces.js");
Components.utils.import("resource://gsync/gsyncgoogle.js");

var g_SyncAPI = {

    synchronize: function(){
    
        LOG.trace("g_SyncAPI#synchronize() BEGIN");
        
        //TODO: errorHandling, progress, userFeedback    
        g_SyncGoogle.doInSecurityContext({
        
            username: "tommysenf@googlemail.com",
            password: "!tommysenf!",
            login: true
        }, function(){
        
            var bookmarksGoogle = g_SyncGoogle.getAllBookmarks();
            var bookmarksPlaces = g_SyncPlaces.getAllBookmarks();
            LOG.trace("g_SyncAPI#synchronize() TODO IMPLEMENT SYNCHRONIZATION!!!");
        });
        
        LOG.trace("g_SyncAPI#synchronize() END")
    },
    
    overrideLocal: function(){
    
        LOG.trace("g_SyncAPI#overrideLocal() BEGIN");
        
        //TODO: errorHandling, progress, userFeedback    
        g_SyncGoogle.doInSecurityContext({
            username: "tommysenf@googlemail.com",
            password: "!tommysenf!",
            login: true
        }, function(){
            var bookmarks = g_SyncGoogle.getAllBookmarks();
            g_SyncPlaces.deleteAllBookmarks();
            g_SyncPlaces.insertBookmarks(bookmarks);
        });
        
        LOG.trace("g_SyncAPI#overrideLocal() END");
    },
    
    overrideRemote: function(){
    
        LOG.trace("g_SyncAPI#overrideRemote() BEGIN");
        
        g_SyncGoogle.doInSecurityContext({
            username: "tommysenf@googlemail.com",
            password: "!tommysenf!",
            login: true
        }, function(){
            var bookmarks = g_SyncPlaces.getAllBookmarks();
            g_SyncGoogle.deleteAllBookmarks();
            g_SyncGoogle.insertBookmarks(bookmarks);
        });
        
        LOG.trace("g_SyncAPI#overrideRemote() END");
    },
    
    
	
	/**
	 * Returns a list of all bookmarks contained in lhsBookarks but not in 
	 * rhsBookmarks. 
	 * 
	 * @param {Object} lhsBookmarks
	 * @param {Object} rhsBookmarks
	 */
    compare: function(lhsBookmarks, rhsBookmarks){
    
        var result = [];
        for (var idxLhs in lhsBookmarks) {
            var found = false;
            for (var rhsIdx in rhsBookmarks) {
                if (rhsBookmarks[rhsIdx].link == lhsBookmarks[lhsIdx].link) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                result.push(lhsBookmarks[lhsIdx]);
            }
        }
        return result;
    }
	
	
}
